package com.icloud.admin.application;


import com.icloud.admin.domain.User;
import com.icloud.admin.domain.UserRepository;

import com.icloud.admin.application.command.AssignRoleToUserCommand;
import com.icloud.admin.application.command.ChangeUserPopsCommand;
import com.icloud.admin.application.command.CreateUserCommand;
import com.icloud.admin.application.command.UndoAssignRoleToUserCommand;
import com.icloud.admin.domain.RoleRepository;
import com.icloud.common.utils.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;


@Service
@Transactional
public class IdentityApplicationService {

    @Autowired
    UserRepository userRepository;

    @Autowired
    RoleRepository roleRepository;

    /**
     * 创建用户
     *
     * @param createUserCommand 创建用户命令
     */
    public void createUser(CreateUserCommand createUserCommand) {

        Assert.isNull(userRepository.findByUsername(createUserCommand.getUsername()), "用户名已存在");
        User user = new User(createUserCommand.getUsername(), createUserCommand.getDescription());
        userRepository.save(user);
    }

    /**
     * 禁用用户
     *
     * @param userId 用户ID
     */
    public void disableUser(String userId) {
        User user = userRepository.findOne(userId);
        if (user != null) {
            user.disable();
        }
    }

    /**
     * 激活用户
     *
     * @param userId 用户id
     */
    public void activateUser(String userId) {
        User user = userRepository.findOne(userId);
        Assert.notNull(user, "用户不存在");
        user.enable();
    }
    /**
     * 更新用户属性
     *
     * @param changeUserPopsCommand 更新用户属性命令
     */
    public void changeUserPops(ChangeUserPopsCommand changeUserPopsCommand) {
        User user = userRepository.findOne(changeUserPopsCommand.getUserId());
        if (user != null) {
            user.changeProps(changeUserPopsCommand.getDescription());
        }
    }

    /**
     * 为用户分配角色
     *
     * @param assignRoleToUserCommand
     */
    public void assignRoleToUser(AssignRoleToUserCommand assignRoleToUserCommand) {
        User user = userRepository.findOne(assignRoleToUserCommand.getUserId());
        if (user != null) {
            List<String> roleIds = assignRoleToUserCommand.getRoleIds();
            for (String roleId:roleIds){
                user.assignRole(roleRepository.findOne(roleId));
            }

        }

    }

    /**
     * 撤销授予用的角色
     *
     * @param undoAssignRoleToUserCommand
     */
    public void undoAssignedRoleToUser(UndoAssignRoleToUserCommand undoAssignRoleToUserCommand) {
        User user = userRepository.findOne(undoAssignRoleToUserCommand.getUserId());
        if (user != null) {
            List<String> roleIds = undoAssignRoleToUserCommand.getRoleIds();
            for (String roleId:roleIds){
                user.undoAssignedRole(roleRepository.findOne(roleId));
            }

        }

    }

}
